﻿using AiXiu.IDAL;
using AiXiu.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;

namespace AiXiu.DAL
{
    /// <summary>
    /// 视频数据服务类
    /// </summary>
    public class VideoService : IVideoService
    {
        /// <summary>
        /// 数据库添加视频
        /// </summary>
        /// <param name="video"></param>
        /// <returns></returns>
        public TBVideos AddVideo(TBVideos video)
        {
            video.Status = (int)VideoStatus.Uploading;
            video.UploadTime = DateTime.Now;
            AiXiuDbContext dbContext = new AiXiuDbContext();
            dbContext.Entry(video).State = EntityState.Added;
            dbContext.SaveChanges();
            return video;
        }


        /// <summary>
        /// 获取处理中的视频Id列表（上传中、上传成功，转码中）
        /// </summary>
        /// <returns></returns>
        public List<string> GetInProcessVideoIds()
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            //获取视频id列表，状态为上传中、上传成功，转码中
            List<TBVideos> videoList= dbContext.TBVideos.Where(a => a.Status == (int)VideoStatus.Uploading || a.Status == (int)VideoStatus.UploadSucces || a.Status == (int)VideoStatus.Transcoding).ToList();
            List<string> videoIds=videoList.Select(a => a.VideoId).ToList();
            return videoIds;
        }


        /// <summary>
        /// 批量更新视频信息
        /// </summary>
        /// <param name="videos">要更新的视频列表</param>
        /// <returns></returns>
        public void UpdateVideos(List<VideoInfo> videoInfos)
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            foreach(VideoInfo videoInfo in videoInfos)
            {
                //TBVideos video= dbContext.TBVideos.SingleOrDefault(a => a.VideoId == videoInfo.VideoId);
                //find方法参数只能是主键
                TBVideos video = dbContext.TBVideos.Find(videoInfo.VideoId);
                video.Status = (int)videoInfo.Status;
                if (!string.IsNullOrEmpty(videoInfo.CoverURL))
                {
                    int lastIndex = videoInfo.CoverURL.IndexOf('?');
                    video.CoverURL = videoInfo.CoverURL.Substring(0, lastIndex);
                }                
                dbContext.Entry(video).State = EntityState.Modified;
                dbContext.SaveChanges();
            }
        }

        /// <summary>
        /// 通过用户id获取视频列表
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public List<TBVideos> GetVideosByUserId(int userId)
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            List<TBVideos> list= dbContext.TBVideos.Where(a => a.UserId == userId).OrderByDescending(a => a.UploadTime).ToList();
            return list;
        }


        /// <summary>
        /// 根据视频id删除视频
        /// </summary>
        /// <param name="videoId">视频id</param>
        public void DeleteVideo(string videoId)
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            TBVideos video= dbContext.TBVideos.Find(videoId);
            //TBVideos video = dbContext.TBVideos.SingleOrDefault(a=>a.VideoId==videoId);
            dbContext.TBVideos.Remove(video);
            //dbContext.Entry(video).State = EntityState.Deleted;
            dbContext.SaveChanges();
        }

        /// <summary>
        /// 获取视频列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<TBVideos> GetVideos()
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            int status = (int)VideoStatus.Normal;
            return dbContext.TBVideos
                .Where(m => m.Status == status)
                .Include(m => m.TBUsers)    // 连接查询用户信息表
                .OrderByDescending(m => m.UploadTime)
                .ToList();
        }

        /// <summary>
        /// 通过视频Id获取视频信息
        /// </summary>
        /// <param name="videoId"></param>
        /// <returns></returns>
        public TBVideos GetVideoByVideoId(string videoId)
        {
            AiXiuDbContext dbContext = new AiXiuDbContext();
            TBVideos video = dbContext.TBVideos
                .Include(m => m.TBUsers)
                .SingleOrDefault(m => m.VideoId == videoId);
            return video;
        }
    }
}